/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：RestHttp.java <br>
 * 创建时间：2015年4月28日 下午5:52:13 <br>
 * 所属项目：GmudEX <br>
 * @author 12548
 */
package lostland.general.network;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.orhanobut.logger.Logger;

import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.ui.LockedScreen;

/**
 * 类名：GmudHttp <br>
 * 说明：
 *
 * @author 12548
 */
public class GmudHttp extends HttpUtils {

	private int lockID = -1;
	private String lockMsg = "正在联网获取数据";

	private GmudRequestCallBack callback;

	public static final String KEY_STATE = "state";

	public static final String STATE_ERROR = "error";
	public static final String STATE_EMPTY = "empty";
	public static final String STATE_OK = "ok";

	/**
	 * @return lockID
	 */
	public int getLockID() {
		return lockID;
	}

	/**
	 * @param lockID
	 * @param lockMsg
	 */
	public GmudHttp(int lockID, String lockMsg) {
		super();
		this.lockID = lockID;
		this.lockMsg = lockMsg;
		callback = new GmudRequestCallBack(lockID);
	}

	/**
	 * @param lockMsg
	 */
	public GmudHttp(String lockMsg) {
		super();
		this.lockMsg = lockMsg;
		callback = new GmudRequestCallBack(lockID);
	}


	public GmudHttp() {
		super();
		callback = new GmudRequestCallBack(lockID);
	}

	public void postLocked(GmudRestUrl url, RequestParams params, GmudRequestListener listener) {

		Logger.i("Posting locked:" + url.getUrl());

		if (params != null) {
			Logger.i(new Gson().toJson(params));
		}

		if (lockID < 0) {
			lockID = LockedScreen.lock(lockMsg);
			callback.setLockID(lockID);
		} else {
			LockedScreen.lock(lockID, lockMsg);
		}
		setListener(listener);
		super.send(HttpMethod.POST, url.getUrl(), params, callback);
	}

	public void postBackground(GmudRestUrl url, RequestParams params, final GmudRequestListener listener) {

		Logger.i("Posting background:" + url.getUrl());

		if (params != null) {
			Logger.i(new Gson().toJson(params));
		}

		setListener(listener);
		super.send(HttpMethod.POST, url.getUrl(), params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				Logger.e(arg1);
				arg0.printStackTrace();
				if (listener != null) {
					listener.onFail(arg0, arg1);
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				if (arg0.statusCode == 200) {
					Logger.i(arg0.result);
				}

				if (listener != null) {
					if (arg0.statusCode == 200) {
						listener.onSucc(arg0);
					} else {
						Logger.e("http return is not 200!!!");
						listener.onFail(new HttpException(), "其他错误！");
					}
				}
			}
		});
	}


	public void setListener(GmudRequestListener listener) {
		callback.setListener(listener);
	}


	public static RequestParams initBattleParams() {
		RequestParams rp = new RequestParams();
		rp.addBodyParameter("arena", BattleScreen.getInstance().battleInfo.getObjectId());
		rp.addBodyParameter("uid", BattleScreen.getInstance().uuid);


		return rp;
	}


}
